import BannerAdInterface from "../base/BannerAdInterface";
export default class WXBanner implements BannerAdInterface {

    private isShow:boolean = true;
    constructor(id){
        this.create(id,(err)=>{
            if(!err){
                if(!this.isShow){
                    this.hide();
                }
            }

        })
    }
    private bannerAd: any = null;
   
    create(id,func: (err:string)=>void) {
        let winSize = wx.getSystemInfoSync();
        
        // console.log(winSize);
        let bannerHeight = 120;
        let bannerWidth = 350;        
        this.destroy();
        this.bannerAd = wx.createBannerAd({
            adUnitId: id,
            style: {
                left:(winSize.windowWidth-bannerWidth)/2,
                top: winSize.windowHeight - bannerHeight,
                width: bannerWidth
            }
        })
        this.bannerAd.onError(err => {
            console.log(err)
            func(err);
        })
        this.bannerAd.onLoad(() => {
            console.log('banner 广告加载成功')
            func(null);
          })        
    }


    show(): void {
        this.isShow = true
        this.bannerAd.show();
    }

    hide(): void {
        this.isShow = false
        this.bannerAd.hide();
    }

    destroy(): void {
        if(this.bannerAd){
            this.bannerAd.destroy()
            this.bannerAd = null;
        }
    }
}
